package com.ruoyi.mateDevice.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheckModel;
import com.ruoyi.mateCheck.domain.MateUserDevice;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateCheck.service.MateCheckService;
import com.ruoyi.mateDevice.domain.DeviceCalibration;
import com.ruoyi.mateDevice.domain.DeviceClean;
import com.ruoyi.mateDevice.domain.DevicePreserve;
import com.ruoyi.mateDevice.domain.DeviceRepair;
import com.ruoyi.mateDevice.domain.DeviceScrap;
import com.ruoyi.mateDevice.domain.MateDevice;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.mateDevice.service.MateDeviceService;
import com.ruoyi.material.domain.MaterialProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateDevice")
public class MateDeviceController extends BaseController {

    @Autowired
    MateDeviceService mateDeviceService;

    @Autowired
    MateCheckService mateCheckService;

    @Autowired
    MateDeviceMapper mateDeviceMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody MateDevice mateDevice) {
        int a = mateDeviceService.insertMateDevice(mateDevice);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @PostMapping("/addRepair")
    public AjaxResult addRepair(@RequestBody DeviceRepair deviceRepair) {
        MateDevice mateDevice = mateDeviceMapper.selectMateDevice(deviceRepair.getMtdeviceId());
        deviceRepair.setRepairWorkshop(mateDevice.getDeviceLocation());
        deviceRepair.setDeviceNum(mateDevice.getDeviceNum());
        deviceRepair.setProductType(mateDevice.getDeviceType());
        deviceRepair.setDeviceName(mateDevice.getDeviceName());
        deviceRepair.setIsProduction(mateDevice.getIsProduction());
        int a = mateDeviceService.addRepair(deviceRepair);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }
    @PostMapping("/addDeviceCalibration")
    public AjaxResult addDeviceCalibration(@RequestBody DeviceCalibration deviceCalibration) {
        MateDevice mateDevice = mateDeviceMapper.selectMateDevice(deviceCalibration.getCalibrationModels().get(0).getMtdeviceId());
        deviceCalibration.setDeptId(SecurityUtils.getDeptId());
        deviceCalibration.setIsProduction(mateDevice.getIsProduction());
        int a = mateDeviceService.addDeviceCalibration(deviceCalibration);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }


    /**
     * 查询校准记录
     */
    @GetMapping("/deviceCalibrationList")
    @ResponseBody
    public TableDataInfo deviceCalibrationList(@RequestParam(value = "isProduction", required = false) String isProduction,
                                      @RequestParam(value = "beginTime", required = false) String beginTime,
                                      @RequestParam(value = "endTime", required = false) String endTime) {

        DeviceCalibration deviceCalibration = new DeviceCalibration();
        deviceCalibration.setDeptId(SecurityUtils.getDeptId());
        deviceCalibration.setIsProduction(isProduction);
        deviceCalibration.setBeginTime(beginTime);
        deviceCalibration.setEndTime(endTime);
        List<DeviceCalibration> list = mateDeviceMapper.deviceCalibrationList(deviceCalibration);
        return getDataTable(list);
    }

    /**
     * 详细查看设备维修记录
     */
    @GetMapping("/selectDeviceCalibration")
    @ResponseBody
    public DeviceCalibration selectDeviceCalibration(@RequestParam(value = "calibrationId", required = false) Long calibrationId) {
        DeviceCalibration deviceCalibration = mateDeviceMapper.selectDeviceCalibration(calibrationId);
        return deviceCalibration;
    }

    @PostMapping("/addScrap")
    public AjaxResult addScrap(@RequestBody DeviceScrap deviceScrap) {
        MateDevice mateDevice = mateDeviceMapper.selectMateDevice(deviceScrap.getMtdeviceId());
        deviceScrap.setUserRoom(mateDevice.getDeviceLocation());
        deviceScrap.setDeptId(SecurityUtils.getDeptId());
        deviceScrap.setDeviceType(mateDevice.getDeviceType());
        deviceScrap.setDeviceName(mateDevice.getDeviceName());
        deviceScrap.setIsProduction(mateDevice.getIsProduction());

        int a = mateDeviceMapper.addScrap(deviceScrap);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    /**
     * 详细查看设备报废申请单
     */
    @GetMapping("/selectDeviceScrap")
    @ResponseBody
    public DeviceScrap selectDeviceScrap(@RequestParam(value = "scrapId", required = false) Long scrapId) {
        DeviceScrap deviceScrap = mateDeviceMapper.selectDeviceScrap(scrapId);
        return deviceScrap;
    }


    @GetMapping("/deviceScrapList")
    @ResponseBody
    public TableDataInfo deviceScrapList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                         @RequestParam(value = "isProduction", required = false) String isProduction,
                                         @RequestParam(value = "endTime", required = false) String endTime) {

        DeviceScrap deviceScrap = new DeviceScrap();
        deviceScrap.setDeptId(SecurityUtils.getDeptId());
        deviceScrap.setIsProduction(isProduction);
        deviceScrap.setBeginTime(beginTime);
        deviceScrap.setEndTime(endTime);
        List<DeviceScrap> list = mateDeviceMapper.deviceScrapList(deviceScrap);
        return getDataTable(list);
    }


    @PostMapping("/addPreserve")
    public AjaxResult addPreserve(@RequestBody DevicePreserve devicePreserve) {
        MateDevice mateDevice = mateDeviceMapper.selectMateDevice(devicePreserve.getMtdeviceId());
        devicePreserve.setProductType(mateDevice.getDeviceType());
        devicePreserve.setUserDept(mateDevice.getDeviceDept());
        devicePreserve.setDeviceName(mateDevice.getDeviceName());
        devicePreserve.setDeviceNum(mateDevice.getDeviceNum());
        devicePreserve.setIsProduction(mateDevice.getIsProduction());
        devicePreserve.setDeptId(SecurityUtils.getDeptId());
        int a = mateDeviceService.addPreserve(devicePreserve);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @PostMapping("/addDeviceClean")
    public AjaxResult addDeviceClean(@RequestBody DeviceClean deviceClean) {
        MateDevice mateDevice = mateDeviceMapper.selectMateDevice(deviceClean.getMtdeviceId());
        deviceClean.setProductType(mateDevice.getDeviceType());
        deviceClean.setDeviceName(mateDevice.getDeviceName());
        deviceClean.setIsProduction(mateDevice.getIsProduction());
        deviceClean.setDeviceNum(mateDevice.getDeviceNum());
        deviceClean.setUserDept(mateDevice.getDeviceDept());
        deviceClean.setDeptId(SecurityUtils.getDeptId());
        int a = mateDeviceService.addDeviceClean(deviceClean);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }


    @GetMapping("/selectEdit")
    public Map<String, Object> selectEdit(@RequestParam(value = "mtdeviceId", required = false) Long mtdeviceId) {
        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> reaMap = new HashMap<>();
        reaMap.put("mtdeviceId", mtdeviceId);
        reaMap.put("deptId", SecurityUtils.getDeptId());
        MateDevice list = mateDeviceMapper.selectMateDeviceListById(reaMap);
        resMap.put("MateDevice", list);
        return resMap;
    }

    @GetMapping("/selectUserInformation")
    public Map<String, Object> selectUserInformation(@RequestParam(value = "mtdeviceId", required = false) Long mtdeviceId,
                                                     @RequestParam(value = "startDate", required = false) String startDate,
                                                     @RequestParam(value = "endDate", required = false) String endDate,
                                                     @RequestParam(value = "isProduction", required = false) String isProduction) {

        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> reaMap = new HashMap<>();
        reaMap.put("mtdeviceId", mtdeviceId);
        reaMap.put("deptId", SecurityUtils.getDeptId());
        reaMap.put("startDate", startDate);
        reaMap.put("endDate", endDate);
        reaMap.put("isProduction", isProduction);
        List<MateUserDevice> list = mateDeviceMapper.selectInformation(reaMap);
        resMap.put("MateDevice", list);
        return resMap;
    }

    @GetMapping("/selectDeviceRepairInformation")
    public Map<String, Object> selectDeviceRepairInformation(@RequestParam(value = "mtdeviceId", required = false) Long mtdeviceId,
                                                         @RequestParam(value = "beginTime", required = false) String beginTime,
                                                         @RequestParam(value = "endTime", required = false) String endTime,
                                                         @RequestParam(value = "isProduction", required = false) String isProduction) {
        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> reaMap = new HashMap<>();
        reaMap.put("mtdeviceId", mtdeviceId);
        reaMap.put("deptId", SecurityUtils.getDeptId());
        reaMap.put("beginTime", beginTime);
        reaMap.put("endTime", endTime);
        reaMap.put("isProduction", isProduction);
        List<DeviceRepair> list = mateDeviceMapper.selectDeviceRepairInformation(reaMap);
        resMap.put("repairInformation", list);
        return resMap;
    }

    @GetMapping("/delete/{mtdeviceId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("mtdeviceId") Long mtdeviceId) {
        mateDeviceMapper.remove(mtdeviceId);
        return AjaxResult.success("删除成功");
    }

    /**
     * 查询设备信息列表
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "deviceName", required = false) String deviceName,
                              @RequestParam(value = "isProduction", required = false) String isProduction) {
        startPage();
        MateDevice mateDevice = new MateDevice();
        mateDevice.setDeviceName(deviceName);
        mateDevice.setIsProduction(isProduction);
        mateDevice.setDeptId(SecurityUtils.getDeptId());
        List<MateDevice> list = mateDeviceService.selectMateDeviceList(mateDevice);
        return getDataTable(list);
    }


    @GetMapping("/allList")
    @ResponseBody
    public List<MateDevice> allList(@RequestParam(value = "isProduction", required = false) String isProduction) {
        MateDevice mateDevice = new MateDevice();
        mateDevice.setIsProduction(isProduction);
        mateDevice.setDeptId(SecurityUtils.getDeptId());
        List<MateDevice> list = mateDeviceService.selectMateDeviceList(mateDevice);
        return list;
    }

    /**
     * 查询设保养记录
     */
    @GetMapping("/perserveList")
    @ResponseBody
    public TableDataInfo perserveList(@RequestParam(value = "isProduction", required = false) String isProduction,
                                      @RequestParam(value = "beginTime", required = false) String beginTime,
                                      @RequestParam(value = "endTime", required = false) String endTime) {

        DevicePreserve devicePreserve = new DevicePreserve();
        devicePreserve.setDeptId(SecurityUtils.getDeptId());
        devicePreserve.setIsProduction(isProduction);
        devicePreserve.setBeginTime(beginTime);
        devicePreserve.setEndTime(endTime);
        List<DevicePreserve> list = mateDeviceService.perserveList(devicePreserve);
        return getDataTable(list);
    }

    /**
     * 查询设备维修记录
     */
    @GetMapping("/repairList")
    @ResponseBody
    public TableDataInfo repairList(@RequestParam(value = "isProduction", required = false) String isProduction,
                                    @RequestParam(value = "beginTime", required = false) String beginTime,
                                    @RequestParam(value = "endTime", required = false) String endTime) {
        startPage();
        DeviceRepair deviceRepair = new DeviceRepair();
        deviceRepair.setBeginTime(beginTime);
        deviceRepair.setEndTime(endTime);
        deviceRepair.setIsProduction(isProduction);
        deviceRepair.setDeptId(SecurityUtils.getDeptId());
        List<DeviceRepair> list = mateDeviceService.selectRepairList(deviceRepair);
        return getDataTable(list);
    }

    /**
     * 查询设备保清洗记录
     */
    @GetMapping("/cleanList")
    @ResponseBody
    public TableDataInfo cleanList(@RequestParam(value = "isProduction", required = false) String isProduction,
                                   @RequestParam(value = "beginTime", required = false) String beginTime,
                                   @RequestParam(value = "endTime", required = false) String endTime) {
        startPage();
        DeviceClean deviceClean = new DeviceClean();
        deviceClean.setBeginTime(beginTime);
        deviceClean.setIsProduction(isProduction);
        deviceClean.setEndTime(endTime);
        deviceClean.setDeptId(SecurityUtils.getDeptId());
        List<DeviceClean> list = mateDeviceService.selectCleanList(deviceClean);
        return getDataTable(list);
    }

    /**
     * 详细查看设备维修记录
     */
    @GetMapping("/selectRepair")
    @ResponseBody
    public DeviceRepair selectRepair(@RequestParam(value = "repairId", required = false) Long repairId) {
        DeviceRepair deviceRepairResult = mateDeviceMapper.selectRepair(repairId);
        return deviceRepairResult;
    }

    /**
     * 详细查看设备保养记录
     */
    @GetMapping("/selectPreserve")
    @ResponseBody
    public DevicePreserve selectPreserve(@RequestParam(value = "preserveId", required = false) Long preserveId) {
        DevicePreserve preserve = mateDeviceMapper.selectPreserve(preserveId);
        return preserve;
    }


    /**
     * 详细查看设备清洁记录
     */
    @GetMapping("/selectDeviceClean")
    @ResponseBody
    public DeviceClean selectDeviceClean(@RequestParam(value = "deviceCleanId", required = false) Long deviceCleanId) {
        DeviceClean deviceClean = mateDeviceMapper.selectDeviceClean(deviceCleanId);
        return deviceClean;
    }


    /**
     * 查询设备信息列表
     */
    @GetMapping("/allDevice")
    @ResponseBody
    public List<MateDevice> allDevice(@RequestParam(value = "deviceName", required = false) String deviceName) {
        MateDevice mateDevice = new MateDevice();
        mateDevice.setDeviceName(deviceName);
        mateDevice.setDeptId(SecurityUtils.getDeptId());
        List<MateDevice> list = mateDeviceService.selectMateDeviceList(mateDevice);
        return list;
    }


    @PostMapping("/edit")
    public AjaxResult updateMateDevice(@RequestBody MateDevice mateDevice) {
        int a = mateDeviceService.updatemateDevice(mateDevice);
        if (a != 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @PostMapping("/editUser")
    public AjaxResult editUser(@RequestBody MateUserDevice mateUserDevice) {
        int a = mateDeviceService.updateUserDevice(mateUserDevice);
        if (a != 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @GetMapping("/selectProduct")
    @ResponseBody
    public List<MaterialProduct> selectProduct() {
        List<MaterialProduct> list = mateDeviceService.selectProduct(SecurityUtils.getLoginUser().getCompanyId());
        return list;
    }

    @GetMapping("/selectUserDevice")
    @ResponseBody
    public TableDataInfo selectUserDevice(@RequestParam(value = "startDate", required = false) String startDate,
                                          @RequestParam(value = "isProduction", required = false) String isProduction,
                                          @RequestParam(value = "endDate", required = false) String endDate) {
        startPage();
        MateUserDevice mateUserDevice = new MateUserDevice();
        mateUserDevice.setDeptId(SecurityUtils.getDeptId());
        mateUserDevice.setBeginTime(startDate);
        mateUserDevice.setEndTime(endDate);
        mateUserDevice.setIsProduction(isProduction);
        List<MateUserDevice> list = mateDeviceService.selectUserDevice(mateUserDevice);
        return getDataTable(list);
    }

    @GetMapping("/selectUserEdit")
    @ResponseBody
    public Map<String, Object> selectUserEdit(@RequestParam(value = "userDeviceId", required = false) Long userDeviceId,
                                              @RequestParam(value = "ckInformationId", required = false) Long ckInformationId,
                                              @RequestParam(value = "mtdeviceId", required = false) Long mtdeviceId) {

        Map<String, Object> resMap = new HashMap<>();
        MateUserDevice mateUserDevice = mateDeviceService.selectUserEdit(userDeviceId);
        MateCheckModel mateCheckModel = new MateCheckModel();
        mateCheckModel.setCkInformationId(ckInformationId);
        mateCheckModel.setMtdeviceId(mtdeviceId);
        List<MateCheckModel> mateCheckModels = mateCheckMapper.selectMateCheckModelDevice(mateCheckModel);
        resMap.put("mateUserDevice", mateUserDevice);
        resMap.put("mateDeviceModels", mateCheckModels);
        return resMap;

    }
}
